using Microsoft.AspNetCore.Mvc;
using WeatherSystem.API.Services;

namespace WeatherSystem.API.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class MetricConfigurationController : ControllerBase
    {
        private readonly IMetricConfigurationService _metricService;
        private readonly ILogger<MetricConfigurationController> _logger;

        public MetricConfigurationController(
            IMetricConfigurationService metricService,
            ILogger<MetricConfigurationController> logger)
        {
            _metricService = metricService;
            _logger = logger;
        }

        /// <summary>
        /// 获取所有可用的指标定义
        /// </summary>
        [HttpGet("available-metrics")]
        public async Task<IActionResult> GetAvailableMetrics(
            [FromQuery] int page = 1,
            [FromQuery] int pageSize = 20,
            [FromQuery] bool? isActive = null)
        {
            var metrics = await _metricService.GetAvailableMetricsAsync(page, pageSize, isActive);
            return Ok(metrics);
        }

        /// <summary>
        /// 获取全局指标配置
        /// </summary>
        [HttpGet("global-config")]
        public async Task<IActionResult> GetGlobalConfig()
        {
            var config = await _metricService.GetGlobalMetricConfigAsync();
            return Ok(config);
        }

        /// <summary>
        /// 更新全局指标配置
        /// </summary>
        [HttpPut("global-config")]
        public async Task<IActionResult> UpdateGlobalConfig([FromBody] Dictionary<string, object> config)
        {
            if (config == null)
            {
                return BadRequest("配置不能为空");
            }

            await _metricService.SetGlobalMetricConfigAsync(config);
            return NoContent();
        }

        /// <summary>
        /// 获取指定城市启用的指标键列表
        /// </summary>
        [HttpGet("cities/{cityId:guid}/metrics")]
        public async Task<IActionResult> GetCityMetrics([FromRoute] Guid cityId)
        {
            var metrics = await _metricService.GetCityMetricsAsync(cityId);
            return Ok(metrics);
        }

        /// <summary>
        /// 设置指定城市的指标键列表
        /// </summary>
        public class UpdateCityMetricsRequest
        {
            public List<string> MetricKeys { get; set; } = new();
        }

        [HttpPut("cities/{cityId:guid}/metrics")]
        public async Task<IActionResult> UpdateCityMetrics([FromRoute] Guid cityId, [FromBody] UpdateCityMetricsRequest request)
        {
            if (request == null || request.MetricKeys == null)
            {
                return BadRequest("请求体不能为空");
            }

            try
            {
                await _metricService.SetCityMetricsAsync(cityId, request.MetricKeys);
                return NoContent();
            }
            catch (ArgumentException ex)
            {
                _logger.LogWarning(ex, "更新城市指标配置失败");
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 获取系统默认启用的指标定义
        /// </summary>
        [HttpGet("default-metrics")]
        public async Task<IActionResult> GetDefaultMetrics()
        {
            var metrics = await _metricService.GetDefaultMetricsAsync();
            return Ok(metrics);
        }
    }
}